﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using model;
using dao;

namespace business
{
    public class BaivietBUS
    {
        LoaibaivietDao loaiBaivietDao = new LoaibaivietDao();
        BaivietDao baivietDao = new BaivietDao();

        public List<BaivietModel> getAllBaiviet()
        {
            return baivietDao.getAllBaiviet();
        }

        public List<BaivietModel> getBaivietTop1ByLoai()
        {
            List<BaivietModel> glsBaiviet = new List<BaivietModel>();
            List<Loaibaiviet> glsLoaibaiviet = new List<Loaibaiviet>();
            glsLoaibaiviet = loaiBaivietDao.GetListAllLoaiBaiviet();
            foreach (Loaibaiviet l in glsLoaibaiviet)
            {
                if (l.Maloaibai.Substring(0, 2) == "MN")
                {
                    List<BaivietModel> glsBv = new List<BaivietModel>();
                    glsBv = baivietDao.getBaivietTop1ByLoai(l.ID);
                    if (glsBv.Count > 0)
                    {
                        BaivietModel bv = glsBv[0];
                        bv.Header = getTenloaibaiviet(l.Maloaibai);
                        glsBaiviet.Add(bv);
                    }
                }
            }
            return glsBaiviet;
        }

        private string getTenloaibaiviet(string s)
        {
            switch (s)
            { 
                case "MN1":
                    return "Trà";
                case "MN2":
                    return "Trà cụ";
                case "MN3":
                    return "Trà thất";
                case "MN4":
                    return "Nghệ thuật thưởng trà";
                case "MN5":
                    return "Văn hóa trà việt";
                case "MN6":
                    return "Tản mạn";
            }
            return "";
        }
        public List<BaivietModel> getBaivietTop3ByLoai(long id)
        {
            List<BaivietModel> glsBaiviet = new List<BaivietModel>();
            List<BaivietModel> glsBaivietResult = new List<BaivietModel>();
            
            glsBaiviet = baivietDao.getTop3BaivietByLoai(baivietDao.getBaivietByID(id)[0].IDLoaibaiviet);
            if (glsBaiviet.Count > 0)
                glsBaiviet.RemoveAt(0);
            if (glsBaiviet.Count >= 2)
            {
                glsBaivietResult.Add(glsBaiviet[0]);
                glsBaivietResult.Add(glsBaiviet[1]);
            }
            else if (glsBaiviet.Count >= 1)
            {
                glsBaivietResult.Add(glsBaiviet[0]);
            }
            return glsBaivietResult;
        }

        public List<BaivietModel> getListBaivietByLoai(long id)
        {
            return baivietDao.getBaivietByLoai(id);
        }

        public string themmoiBaiviet(BaivietModel bv)
        {
            return baivietDao.themmoiBaiviet(bv);
        }

        public string suaBaiviet(BaivietModel bv)
        {
            return baivietDao.suaBaiviet(bv);
        }

        public List<BaivietModel> getListBaivietByID(long id)
        {
            return baivietDao.getBaivietByID(id);
        }

        public List<BaivietModel> getList10Baiviet(long id, long idloai)
        {
            List<BaivietModel> lstBaiviet = new List<BaivietModel>();
            List<BaivietModel> lstBaivietResult = new List<BaivietModel>();
            lstBaiviet = baivietDao.getBaivietByLoai(idloai);
            if (lstBaiviet.Count > 10)
            {
                for (int i = 0; i < 10; i++)
                {
                    if(id != lstBaiviet[i].ID)
                        lstBaivietResult.Add(lstBaiviet[i]);
                }
                return lstBaivietResult;
            }
            else {
                for (int i = 0; i < lstBaiviet.Count; i++)
                {
                    if (id != lstBaiviet[i].ID)
                        lstBaivietResult.Add(lstBaiviet[i]);
                }
                return lstBaivietResult;
            }
        }
    }
}
